home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / O Boy / Source / events.c < prev    next >
Text File  |  1996-06-21  |  9KB  |  431 lines

  1. // 
  2. //    events.h ... part of the shell from C.Spathis - TY Gus
  3. //
  4. #include <AppleEvents.h>
  5. #include <AERegistry.h>
  6. #include <EPPC.h>
  7. #include <quickdraw.h>
  8. #include <PictUtil.h>
  9. #include <Drag.h>
  10. #include "O Boy.h"
  11. #include "Helpers_ut.h"
  12. #include "debug.h"
  13. #include "OBoy_hi.h"
  14.  
  15. #define    kEndOfList            nil            // Nil terminator for variable argument list.
  16.  
  17. //#define topLeft(r)    (((Point*)&(r))[0])
  18. //#define botRight(r)    (((Point*)&(r))[1])
  19. // Drag Manager Crap
  20. void DoStartDrag(CWindowPtr window, EventRecord *event);
  21. pascal OSErr BogusSendProc(FlavorType theType,  void *dragSendRefCon,
  22.                           ItemReference theItemRef, DragReference theDragRef);
  23. OSErr DoMakeHFSPromise(DragReference dragRefNum, ItemReference itemRefNum);
  24. void OutlineRegion(RgnHandle theRgn);
  25. OSErr GetDropLocationDirectory(AEDesc *dropLocation, long *directoryID, short *volumeID);
  26.                           
  27.  
  28. void HandleContentClick(CWindowPtr theWin,EventPtr theEvent);
  29. void HandleNullEvent(EventPtr theEvent);
  30. void HandleMouseDownEvent(EventPtr theEvent);
  31. void HandleMouseUpEvent(EventPtr theEvent);
  32. void HandleKeyDownEvent(EventPtr theEvent);
  33. void HandleKeyUpEvent(EventPtr theEvent);
  34. void HandleAutoKeyEvent(EventPtr theEvent);
  35. void HandleUpdateEvent(EventPtr theEvent);
  36. void HandleDiskEvent(EventPtr theEvent);
  37. void HandleActivateEvent(EventPtr theEvent);
  38. void HandleOSEvent(EventPtr theEvent);
  39. void HandleHiLevelEvent(EventPtr theEvent);
  40.  
  41. void MenuHandler(EventPtr theEvent,short theMenu,short theItem);
  42.  
  43. void HandleFileMenu(EventPtr theEvent,short theItem);
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. Boolean        gDone=FALSE;
  52. Rect        limitRect;
  53. Rect        sizeRect;
  54. RGBColor    white={65535,65535,65535},black={0,0,0};
  55.  
  56. void EventInit(void)
  57. {
  58.     limitRect=qd.screenBits.bounds;
  59.     InsetRect(&limitRect,4,4);
  60.     sizeRect.top=312;
  61.     sizeRect.left=260;
  62.     sizeRect.bottom=qd.screenBits.bounds.bottom-qd.screenBits.bounds.top;
  63.     sizeRect.right=qd.screenBits.bounds.right-qd.screenBits.bounds.left;
  64. }
  65. Boolean DoEvents(void)
  66. {
  67.     Boolean     ret=FALSE;
  68.     Boolean        wret;
  69.     EventRecord    theEvent;
  70.     
  71.     if (bHaveWaitNextEvent){
  72.         wret=WaitNextEvent(everyEvent,&theEvent,sleepTime,nil);
  73.     }else{
  74.         GetNextEvent(everyEvent,&theEvent);
  75.         SystemTask();
  76.     }
  77.     switch(theEvent.what){
  78.         case nullEvent:
  79.             HandleNullEvent(&theEvent);
  80.             break;
  81.         case mouseDown:
  82.             HandleMouseDownEvent(&theEvent);
  83.             break;
  84.         case mouseUp:
  85.             HandleMouseUpEvent(&theEvent);
  86.             break;
  87.         case keyDown:
  88.             HandleKeyDownEvent(&theEvent);
  89.             break;
  90.         case keyUp:
  91.             HandleKeyUpEvent(&theEvent);
  92.             break;
  93.         case autoKey:
  94.             HandleAutoKeyEvent(&theEvent);
  95.             break;
  96.         case updateEvt:
  97.             HandleUpdateEvent(&theEvent);
  98.             break;
  99.         case diskEvt:
  100.             HandleDiskEvent(&theEvent);
  101.             break;
  102.         case activateEvt:
  103.             HandleActivateEvent(&theEvent);
  104.             break;
  105.         case osEvt:
  106.             HandleOSEvent(&theEvent);
  107.             break;
  108.         case kHighLevelEvent:
  109.             HandleHiLevelEvent(&theEvent);
  110.             break;
  111.         default:
  112.             break;        
  113.     }
  114.     ret=GetQuitStatus();
  115.     return ret;
  116. }
  117. void HandleNullEvent(EventPtr theEvent)
  118. {
  119.     ;
  120. }
  121. void HandleMouseDownEvent(EventPtr theEvent)
  122. {
  123.     short        where;
  124.     WindowPtr    theWin;
  125.     Rect        r,theRect;
  126.     GrafPtr        savePort;
  127.     
  128.     where=FindWindow(theEvent->where,&theWin);
  129.     switch(where){
  130.         case inDesk:
  131.             break;
  132.         case inMenuBar:
  133.             {
  134.                 long theMenuItem;
  135.                 short    theMenu,theItem;
  136.                 
  137.                 theMenuItem=MenuSelect(theEvent->where);
  138.                 theMenu=HiWord(theMenuItem);
  139.                 theItem=LoWord(theMenuItem);
  140.                 if (theMenu!=0){
  141.                     MenuHandler(theEvent,theMenu,theItem);
  142.                 }    
  143.             }
  144.             break;
  145.         case inSysWindow:
  146.             break;
  147.         case inContent:
  148.             if (theWin!=nil){
  149.                 if (theWin!=FrontWindow()){
  150.                     SelectWindow(theWin);
  151.                 }else{
  152.                     HandleContentClick((CWindowPtr)theWin,theEvent);
  153.                 }
  154.             }
  155.             break;
  156.         case inDrag:
  157.             if (theWin!=nil){
  158.                 if (theWin!=FrontWindow()){
  159.                     SelectWindow(theWin);
  160.                 }
  161.                 DragWindow(theWin,theEvent->where,&limitRect);
  162.                 if (theWin!=FrontWindow()){
  163.                     SelectWindow(theWin);
  164.                     // DrawGrowIcon(theWin);
  165.                 }
  166.             }else{
  167.                 SysBeep(10);
  168.             }
  169.             break;
  170.         case inGrow:
  171.             if (theWin!=nil){
  172.                 long    newWinSize;
  173.  
  174.                 GetPort(&savePort);
  175.                 SetPort(theWin);
  176.                 if (theWin!=FrontWindow()){
  177.                     SelectWindow(theWin);
  178.                 }
  179.                 Rect theOldRect=((WindowPeek)theWin)->port.portRect;
  180.  
  181.                 newWinSize=GrowWindow(theWin,theEvent->where,&sizeRect);
  182.                 if (newWinSize!=0){                    
  183.                     theRect=((WindowPeek)theWin)->port.portRect;
  184.                     SizeWindow(theWin,LoWord(newWinSize),HiWord(newWinSize),true);
  185.                     SetRect(&r,theRect.left,theRect.bottom-15,theRect.right,theRect.bottom);
  186.                     EraseRect(&r);
  187.                     InvalRect(&r);
  188.                     
  189.                     SetRect(&r,theRect.right-15,theRect.top,theRect.right,theRect.bottom);
  190.                     EraseRect(&r);
  191.                     InvalRect(&r);
  192.                     // inform the hi
  193.                     Rect theNewRect=((WindowPeek)theWin)->port.portRect;
  194.                     OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWin );
  195.                     dassert( theHI != nil );
  196.                     theHI->AdjustToSize( theOldRect, theNewRect );
  197.  
  198.                 }
  199.                 SetPort(savePort);
  200.             }
  201.             break;
  202.         case inGoAway:
  203.             if (theWin!=nil){
  204.                 if (theWin!=FrontWindow()){
  205.                     SelectWindow(theWin);
  206.                 }else if (TrackGoAway(theWin,theEvent->where)==TRUE){
  207.                     OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWin );
  208.                     if( theHI != nil )
  209.                     {
  210.                         delete theHI;
  211.                     }
  212.                 }
  213.             }else{
  214.                 SysBeep(10);
  215.             }
  216.             break;
  217.         case inZoomIn:
  218.         case inZoomOut:
  219.             if (theWin!=nil){
  220.                 if (theWin!=FrontWindow()){
  221.                     SelectWindow(theWin);
  222.                 }else{
  223.                     GrafPtr        savePort;
  224.                     
  225.                     GetPort(&savePort);
  226.                     SetPort(theWin);
  227.                     Rect theOldRect=((WindowPeek)theWin)->port.portRect;
  228.     
  229.                         ZoomWindow(theWin,where,true);
  230.  
  231.                     Rect theNewRect=((WindowPeek)theWin)->port.portRect;
  232.                     OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWin );
  233.                     dassert( theHI != nil );
  234.                     theHI->AdjustToSize( theOldRect, theNewRect );
  235.  
  236.                     SetPort(savePort);
  237.                 }
  238.             }
  239.         default:
  240.             break;
  241.     }
  242. }
  243. void HandleContentClick(CWindowPtr theWin,EventPtr theEvent)
  244. {
  245.     GrafPtr    save_port;
  246.     GetPort( &save_port );
  247.     SetPort( (GrafPtr) theWin );
  248.  
  249.         // oboy specific
  250.         OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWin );
  251.         dassert( theHI != nil );
  252.         theHI->HandleClick( theEvent );
  253.  
  254.     SetPort( save_port );
  255.  
  256. }
  257.  
  258. void HandleMouseUpEvent(EventPtr theEvent)
  259. {
  260.     ;
  261. }
  262. void HandleKeyDownEvent(EventPtr theEvent)
  263. {
  264.     char    theKey;
  265.     Boolean    processed=false;
  266.     
  267.     theKey=theEvent->message & charCodeMask;
  268.     if ( (theEvent->modifiers & cmdKey) !=0){
  269.         long     menuResult;
  270.         short    theMenu,theItem;
  271.         
  272.         menuResult=MenuKey(theKey);
  273.         theMenu=HiWord(menuResult);
  274.         theItem=LoWord(menuResult);
  275.         if (theMenu!=0){
  276.             HiliteMenu(theMenu);
  277.             MenuHandler(theEvent,theMenu,theItem);
  278.             processed=true;
  279.         }
  280.     }
  281.     if (processed==false)
  282.     {
  283.         WindowPtr     theWin=FrontWindow();
  284.         if( theWin != nil )
  285.         {
  286.             
  287.             OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWin );
  288.             if( theHI != nil )
  289.             {
  290.                 theHI->HandleKeyDownEvent(*theEvent);
  291.             }
  292.         }
  293.  
  294.     }
  295.         
  296. }
  297. void HandleKeyUpEvent( EventPtr theEvent)
  298. {
  299.     ;
  300. }
  301. void HandleAutoKeyEvent( EventPtr theEvent)
  302. {
  303.     ;
  304. }
  305.  
  306.  
  307. void HandleUpdateEvent( EventPtr theEvent)
  308. {
  309.     GrafPtr        savePort;
  310.     WindowPtr     theWin=(WindowPtr)theEvent->message;
  311.     Rect        theRect=((WindowPeek)theWin)->port.portRect;
  312.     WinInfoHandle    theH;
  313.     
  314.     GetPort(&savePort);
  315.     SetPort(theWin);
  316.     BeginUpdate(theWin);
  317.             RGBForeColor(&black);
  318.             RGBBackColor(&OBoy_hi::_BackGroundGray);
  319.             EraseRect(&theRect);
  320.             if (theWin!=nil){
  321.                 theH=(WinInfoHandle)((WindowPeek)theWin)->refCon;
  322.                 OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( theWin );
  323.                 dassert( theHI != nil );
  324.                 theHI->ReDraw(  );
  325.             }
  326.         //    DrawGrowIcon(theWin);
  327.     EndUpdate(theWin);
  328.     SetPort(savePort);
  329. }
  330. void HandleDiskEvent(EventPtr theEvent)
  331. {
  332.     ;
  333. }
  334. void HandleActivateEvent(EventPtr theEvent)
  335. {
  336.     InitCursor();
  337.     
  338.     Rect theRect;
  339.     WindowPtr    theW=(WindowPtr)theEvent->message;
  340.     if( theW != nil )
  341.     {
  342.         SetRect( &theRect,theW->portRect.right-15,
  343.                      theW->portRect.bottom-15,
  344.                      theW->portRect.right,
  345.                      theW->portRect.bottom );
  346.         StPort    thePort( theW );
  347.         InvalRect( &theRect );
  348.     }
  349. }
  350. void HandleOSEvent(EventPtr theEvent)
  351. {
  352.     if (theEvent->message & 0x01000000){
  353.         if (theEvent->message & 0x00000001){
  354.             WindowPtr    theW=FrontWindow();
  355.             if (theW!=nil){
  356.                 //DrawGrowIcon(theW);    // Activate Event
  357.             }
  358.         }else{
  359.             ;    // Deactivate Event
  360.         }
  361.     }
  362.     if ((theEvent->message&0xFF000000)==0xFA000000){
  363.         ;     // Cursor Maintenance
  364.     }
  365. }
  366. void HandleHiLevelEvent(EventPtr theEvent)
  367. {
  368.     AEProcessAppleEvent(theEvent);    // Let the Apple Event Manager handle high level event.
  369. }
  370.  
  371. void MenuHandler(EventPtr theEvent,short theMenu,short theItem)
  372. {
  373.     Str255    daName;
  374.     GrafPtr    savePort;
  375.     
  376.     switch(theMenu){
  377.         case APPLE_MENU:
  378.             switch(theItem){
  379.                 case 1:
  380.                     OBoy_hi::ShowAbout();
  381.                     break;
  382.                 default:
  383.                     GetPort(&savePort);
  384.                     GetItem(GetMHandle(APPLE_MENU), theItem, daName);
  385.                     OpenDeskAcc(daName);
  386.                     SetPort(savePort);
  387.                     break;
  388.             }
  389.             break;
  390.         case FILE_MENU:
  391.             HandleFileMenu(theEvent,theItem);
  392.             break;
  393.         default:
  394.             while(!Button());
  395.             break;
  396.     }
  397.     HiliteMenu(0);
  398. }
  399. void HandleFileMenu(EventPtr theEvent,short theItem)
  400. {
  401.     switch(theItem){
  402.         case 1:
  403.             OBoy_hi    *theHumanInterface = new OBoy_hi;
  404.             break;
  405.             
  406.         case 3:
  407.             QuitApp();
  408.             break;
  409.         default:
  410.             break;
  411.     }
  412. }
  413.  
  414. void QuitApp(void)
  415. {
  416.     // dispose of the list handle
  417.     //    WindowPtr    theWindow = FrontWindow();
  418.     //    OBoy_hi *theHI = (OBoy_hi *) GetWRefCon( (GrafPtr)theWindow );
  419.     //    dassert( theHI != nil );
  420.     //    delete theHI;
  421.  
  422.     gDone=TRUE;
  423. }
  424. Boolean GetQuitStatus()
  425. {
  426.     return gDone;
  427. }
  428.  
  429.  
  430.  
  431.